package gui;

import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.sql.*;

import javax.swing.JOptionPane;

import database.DBconn;

public class RegisterMenu extends JPanel {
	private JTextField textFieldName;
	private JTextField textFieldDOB;
	private JTextField textFieldEmail;
	private JTextField textFieldUsername;
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;

	/**
	 * Create the panel.
	 */
	
	 File pics;
	 FileInputStream fistream;
	 PreparedStatement stmt = null;	 
	
	public RegisterMenu(JPanel loginpanel) {
		loginpanel.removeAll();
		loginpanel.setLayout(null);
		
		final JPanel panel = new JPanel();
		panel.setBounds(0, 0, 318, 587);
		loginpanel.add(panel);
		panel.setLayout(null);
		
		textFieldName = new JTextField();
		textFieldName.setBounds(104, 92, 173, 20);
		panel.add(textFieldName);
		textFieldName.setColumns(10);
		
		textFieldDOB = new JTextField();
		textFieldDOB.setBounds(104, 136, 173, 20);
		panel.add(textFieldDOB);
		textFieldDOB.setColumns(10);
		
		textFieldEmail = new JTextField();
		textFieldEmail.setBounds(104, 181, 173, 20);
		panel.add(textFieldEmail);
		textFieldEmail.setColumns(10);
		
		textFieldUsername = new JTextField();
		textFieldUsername.setBounds(104, 227, 173, 20);
		panel.add(textFieldUsername);
		textFieldUsername.setColumns(10);
		
		JLabel lblName = new JLabel("Name");
		lblName.setBounds(23, 95, 71, 14);
		panel.add(lblName);
		
		JLabel lblDOB = new JLabel("DOB");
		lblDOB.setBounds(23, 139, 71, 14);
		panel.add(lblDOB);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(23, 184, 71, 14);
		panel.add(lblEmail);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(23, 230, 71, 14);
		panel.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(23, 271, 71, 14);
		panel.add(lblPassword);
		
		JLabel lblCPassword = new JLabel("<html>Conform <br> Password</html>");
		lblCPassword.setBounds(23, 311, 71, 26);
		panel.add(lblCPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(104, 268, 173, 20);
		panel.add(passwordField);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(104, 307, 173, 20);
		panel.add(passwordField_1);
		
		JLabel lblPicture = new JLabel("<html>User<br>Picture </html>");
		lblPicture.setBounds(23, 348, 46, 32);
		panel.add(lblPicture);
		
		JButton btnSelectPicture = new JButton("Select Picture");
		btnSelectPicture.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try
				  {
				     JFileChooser picChooser = new JFileChooser();    //creates the JFileChooser object
				     int returnVal = picChooser.showOpenDialog(null); //Opens the JFileChooser window
				     File file = null;
				     if(returnVal  == JFileChooser.APPROVE_OPTION)     //Confirmation for selected file
				     {
				        file  = picChooser.getSelectedFile();             //Returns the selected file.
				        String filename=file.getAbsolutePath();          //Returns the path of the file.
				        pics=new File(filename);              //File pics; (Define as global variable).
				        fistream=new FileInputStream(pics);  //FileInputStream fistream ; (Define as global  variable).
				     }
				  }
				  catch(Exception  ex)
				  {
				        ex.printStackTrace();
				  }
				
			}
		});
		btnSelectPicture.setBounds(104, 355, 173, 23);
		panel.add(btnSelectPicture);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try
				{
				  String  name=textFieldName.getText();
				  String  dob=textFieldDOB.getText();
				  String  email=textFieldEmail.getText();
				  String  username=textFieldUsername.getText();
				  String  password=textFieldUsername.getText();
		        //  String password= new String (passwordField.getPassword());
				
				  int code = JOptionPane.showConfirmDialog(null, "Information of " + name + " will be added in database.", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
				  if(code==JOptionPane.YES_OPTION)
				  {
				    //SQL query  for insert into
					DBconn.getConnection();
					stmt = DBconn.con.prepareStatement("insert into userinfo(u_name,u_dob,u_email,u_username,u_password,u_picture)"+"values(?,?,?,?,?,?)");
				
					stmt.setString(1,name);
					stmt.setString(2,dob);
					stmt.setString(3,email);
					stmt.setString(4,username);
					stmt.setString(5,password);
				    //Convert the image in Binary  format
					stmt.setBinaryStream(6, (InputStream)fistream, (int)(pics.length()));
				    //Code for updating the table
				    int  success = stmt.executeUpdate();
				    if(success>0)
				    {
				      JOptionPane.showMessageDialog(null,  "Image Uploaded");
				    }
				    else
				    {
				      JOptionPane.showMessageDialog(null,  "Problem is uploading");
				    }
				  }
				}
				catch(Exception ex)
				{
				  ex.printStackTrace();
				}
				
			}
		});
		btnRegister.setBounds(188, 434, 89, 23);
		panel.add(btnRegister);
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				new MaskLoginMenu(panel).setVisible(true);
				
			}
		});
		btnBack.setBounds(23, 434, 89, 23);
		panel.add(btnBack);
		
		JLabel lblWelcome = new JLabel("Welcome");
		lblWelcome.setBounds(135, 43, 62, 14);
		panel.add(lblWelcome);				
		
		loginpanel.validate();
		loginpanel.repaint();

	}
}
